package com.novel.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.novel.base.entity.R;
import com.novel.web.entity.NfChapter;
import com.novel.web.service.NfChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 章节表
 *
 * @author 樊温军
 * @date Fri May 09 15:15:28 CST 2025
 */
@Slf4j
@Api(tags = "章节表", value = "章节表")
@RestController
@AllArgsConstructor
@RequestMapping("/nfChapter")
public class NfChapterController {

    private final NfChapterService nfChapterService;

    /**
     * 章节表—列表
     *
     * @param nfChapter 章节表
     * @return R<List < NfChapter>>
     */
    @ApiOperation(value = "章节表—列表", notes = "章节表—列表")
    @GetMapping("/list")
    public R<List<NfChapter>> getNfChapterList(NfChapter nfChapter) {
        log.info("开始查询章节表—列表，参数：{}", nfChapter);
        try {
            return R.ok(nfChapterService.getNfChapterList(nfChapter), "查询成功");
        } catch (Exception e) {
            log.error("查询章节表—列表时发生错误：{}", e.getMessage(), e);
            return R.failed("查询章节表—列表时发生错误，请稍后重试！");
        }
    }

    /**
     * 章节表—列表—分页
     *
     * @param page      分页对象
     * @param nfChapter 章节表
     * @return R<IPage < NfChapter>>
     */
    @ApiOperation(value = "章节表—列表—分页", notes = "章节表—列表—分页")
    @GetMapping(value = "/page")
    public R<IPage<NfChapter>> getNfChapterPage(Page<NfChapter> page, NfChapter nfChapter) {
        log.info("开始查询章节表—列表—分页，第 {} 页，每页 {} 条记录，其他参数：{}", page.getCurrent(), page.getSize(), nfChapter);
        try {
            IPage<NfChapter> iPage = nfChapterService.getPage(page, nfChapter);
            return R.ok(iPage, "查询成功");
        } catch (Exception e) {
            log.error("查询章节表—列表—分页时发生错误：{}", e.getMessage(), e);
            return R.failed("查询章节表—列表—分页时发生错误，请稍后重试！");
        }
    }

    /**
     * 章节表—保存
     *
     * @param nfChapter 章节表
     * @return R<Long>
     */
    @ApiOperation(value = "章节表—保存", notes = "章节表—保存")
    @PostMapping(value = "/saveOne")
    public R<Long> saveOneNfChapter(@Valid @RequestBody NfChapter nfChapter) {
        log.info("开始保存章节表：{}", nfChapter);
        try {
            Long id = nfChapterService.saveOneNfChapter(nfChapter);
            log.info("章节表保存成功，ID: {}", id);
            return R.ok(id, "保存成功");
        } catch (Exception e) {
            log.error("保存章节表时发生错误：{}", e.getMessage(), e);
            return R.failed("保存章节表时发生错误，请稍后重试！");
        }
    }

    /**
     * 章节表—删除
     *
     * @param id 章节表主键
     * @return R<Boolean>
     */
    @ApiOperation(value = "章节表—删除", notes = "章节表—删除")
    @PostMapping(value = "/delById")
    public R<Boolean> delByIdNfChapter(Long id) {
        log.info("开始删除章节表：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            nfChapterService.delById(id);
            return R.ok(Boolean.TRUE, "删除成功");
        } catch (Exception e) {
            log.error("删除章节表失败: ID={}, Error={}", id, e.getMessage(), e);
            return R.failed("删除失败，请稍后重试！");
        }
    }

    /**
     * 章节表—详情
     *
     * @param id 章节表主键
     * @return R<NfChapter>
     */
    @ApiOperation(value = "章节表—详情", notes = "章节表—详情")
    @GetMapping(value = "/details")
    public R<NfChapter> getNfChapter(Long id) {
        log.info("开始查询章节表：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            NfChapter nfChapter = nfChapterService.queryById(id);
            if (nfChapter != null) {
                return R.ok(nfChapter, "查询成功");
            } else {
                return R.failed("未找到章节表");
            }
        } catch (Exception e) {
            log.error("查询章节表详情时发生错误：{}", e.getMessage(), e);
            return R.failed("查询章节表详情时发生错误，请稍后重试！");
        }
    }
}
